import {App, Modal, Setting} from "obsidian";

export default class KeyModal extends Modal {
	onsubmit: (key: string) => void;

	constructor(app: App, onsubmit: (key: string) => void) {
		super(app);
		this.onsubmit = onsubmit;
	}

	onOpen() {
		const { contentEl } = this;
		contentEl.createEl('h2', { text: 'Enter Encryption/Decryption Key' });

		let keyInput: HTMLInputElement;

		new Setting(contentEl)
			.addText(text => {
				keyInput = text.inputEl;
				keyInput.type = 'password'; // 设置输入框为密码类型
				text.setPlaceholder('Enter your key here')
					.onChange(() => {}); // 可以在这里处理输入值的变化
			});

		new Setting(contentEl)
			.addButton(btn => {
				btn.setButtonText('Submit')
					.onClick(() => {
						if (keyInput) {
							this.onsubmit(keyInput.value);
						}
						this.close();
					});
			});
	}

	onClose() {
		const { contentEl } = this;
		contentEl.empty();
	}
}
